package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.entity.ProColor;
import com.mall.product.domain.entity.ProColorGroup;
import com.mall.product.service.ProColorGroupService;
import com.mall.product.service.ProColorService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 颜色Controller
 *
 * @author zhonglin
 * @date 2020-04-29
 */
@Controller
@RequestMapping("/product/color")
public class ProColorController
    extends BaseController {

  private String prefix = "product/color" ;

  @Autowired
  private ProColorService proColorService;
  @Autowired
  private ProColorGroupService proColorGroupService;

  @RequiresPermissions("product:color:view")
  @GetMapping()
  public String color() {

    return prefix + "/color" ;
  }

  /**
   * 查询颜色列表
   */
  @RequiresPermissions("product:color:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProColor proColor) {

    startPage();
    List<ProColor> list = proColorService.selectProColorList(proColor);
    return getDataTable(list);
  }

  /**
   * 导出颜色列表
   */
  @RequiresPermissions("product:color:export")
  @Log(title = "颜色", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProColor proColor) {

    List<ProColor>      list = proColorService.selectProColorList(proColor);
    ExcelUtil<ProColor> util = new ExcelUtil<ProColor>(ProColor.class);
    return util.exportExcel(list, "color");
  }

  /**
   * 新增颜色
   */
  @GetMapping("/add")
  public String add(ModelMap mmap) {

    mmap.put("colorGroups", proColorGroupService.selectColorGroupAll());
    return prefix + "/add" ;
  }

  /**
   * 新增保存颜色
   */
  @RequiresPermissions("product:color:add")
  @Log(title = "颜色", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProColor proColor) {

    return toAjax(proColorService.insertProColor(proColor));
  }

  /**
   * 修改颜色
   */
  @GetMapping("/edit/{colorId}")
  public String edit(@PathVariable("colorId") Long colorId,
                     ModelMap mmap) {

    ProColor proColor = proColorService.selectProColorById(colorId);
    ProColorGroup proColorGroup = proColorGroupService.selectProColorGroupById(proColor.getColorGroupId());
    mmap.put("proColor", proColor);
    mmap.put("colorGroup", proColorGroup);

    return prefix + "/edit" ;
  }

  /**
   * 修改保存颜色
   */
  @RequiresPermissions("product:color:edit")
  @Log(title = "颜色", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProColor proColor) {

    return toAjax(proColorService.updateProColor(proColor));
  }

  /**
   * 删除颜色
   */
  @RequiresPermissions("product:color:remove")
  @Log(title = "颜色", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proColorService.deleteProColorByIds(ids));
  }

}
